package co.jp.roomy;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import co.jp.roomy.vo.Heya;
import co.jp.roomy.vo.MyKagu;

public class MadoriActivity extends Activity implements OnTouchListener {

	private Heya heya;
	private DatabaseHelper helper = new DatabaseHelper(this);
	public static SQLiteDatabase db;

	// ImageView target;
	// ImageView target2;
	Map<Integer, ImageView> targetMap = new HashMap<Integer, ImageView>();
	Map<Integer, MyKagu> myKaguMap = new HashMap<Integer, MyKagu>();

	int startX;
	int startY;
	int currentX;
	int currentY;
	int offsetX;
	int offsetY;
	int offsetX1;
	int offsetY1;
	int offsetX2;
	int offsetY2;
	boolean firstFlg = true;
	List<MyKagu> myKaguList = new ArrayList<MyKagu>();

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.madori);
		Intent intent = getIntent();
		heya = (Heya) intent.getSerializableExtra(MadoriActivity.this
				.getString(R.string.heyaKey));

		Resources r = getResources();
		Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.table);

		/** 違う **/
		/** List<MyKagu> myKaguList = heya.getMykaguList(); **/
		db = helper.getReadableDatabase();
		String[] columns = { "myKaguPosX", "myKaguPosY", "myKaguId" };
		String orderBy = "myKaguId ASC";
		String[] selections = new String[1];
		selections[0] = heya.getHeyaId().toString();
		Cursor c = db.query("T_MYKAGU", columns, "heyaId = ?", selections,
				null, null, orderBy);
		startManagingCursor(c);

		for (int i = 0; i < c.getCount(); i++) {
			c.moveToNext();
			MyKagu myKagu = new MyKagu();
			myKagu.setMyKaguPosX(c.getInt(c.getColumnIndex("myKaguPosX")));
			myKagu.setMyKaguPosY(c.getInt(c.getColumnIndex("myKaguPosY")));
			myKagu.setMyKaguId(c.getInt(c.getColumnIndex("myKaguId")));
			myKaguList.add(myKagu);
		}
		c.close();

		FrameLayout frameLayout = (FrameLayout) findViewById(R.id.FrameLayout01);

		for (MyKagu myKagu : myKaguList) {
			ImageView target = new ImageView(this);
			target.setImageBitmap(bmp);
			frameLayout
					.addView(target, new ViewGroup.LayoutParams(bmp.getWidth(),
							bmp.getHeight()));
			target.setTag(myKagu.getMyKaguId());
			target.layout(myKagu.getMyKaguPosX(), myKagu.getMyKaguPosY(),
					myKagu.getMyKaguPosX() + bmp.getWidth(),
					myKagu.getMyKaguPosY() + bmp.getHeight());
			target.setOnTouchListener(this);

			targetMap.put(myKagu.getMyKaguId(), target);
			myKaguMap.put(myKagu.getMyKaguId(), myKagu);
		}

		// target = (ImageView) findViewById(R.id.ImageView01);
		// target.setImageBitmap(bmp);
		// target.setTag("hoge");
		// this.target.setOnTouchListener(this);
		//
		// target2 = (ImageView) findViewById(R.id.ImageView02);
		// target2.setImageBitmap(bmp);
		// target2.setTag("huga");
		// target2.layout(100, 100, 120, 120);
		// this.target2.setOnTouchListener(this);

		((Button) findViewById(R.id.clickMenu))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						// Menu の表示
						openOptionsMenu();
					}
				});
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		if (firstFlg) {
			startX = (int) event.getRawX();
			startY = (int) event.getRawY();
			firstFlg = false;
		}

		int tag = (Integer) v.getTag();
		ImageView target = targetMap.get(tag);
		MyKagu myKagu = myKaguMap.get(tag);
		offsetX = myKagu.getMyKaguPosX();
		offsetY = myKagu.getMyKaguPosY();

		// if (tag.equals("huga")) {
		// offsetX = offsetX1;
		// offsetY = offsetY1;
		// } else if (tag.equals("huga")){
		// offsetX = offsetX2;
		// offsetY = offsetY2;
		// }

		int x = (int) event.getRawX();
		int y = (int) event.getRawY();

		if (event.getAction() == MotionEvent.ACTION_MOVE) {
			currentX = offsetX - startX;
			currentY = offsetY - startY;
			v.layout(currentX, currentY, currentX + v.getWidth(),
					currentY + v.getHeight());
			offsetX = x;
			offsetY = y;

		} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
			offsetX = x;
			offsetY = y;
		}

		// if (tag.equals("hoge")) {
		// offsetX1 = offsetX;
		// offsetY1 = offsetY;
		// } else if (tag.equals("huga")){
		// offsetX2 = offsetX;
		// offsetY2 = offsetY;
		// }

		myKagu.setMyKaguPosX(offsetX);
		myKagu.setMyKaguPosY(offsetY);
		myKaguMap.put(tag, myKagu);

		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		boolean ret = super.onCreateOptionsMenu(menu);

		// メニューインフレーターを取得
		MenuInflater inflater = getMenuInflater();
		// xmlのリソースファイルを使用してメニューにアイテムを追加
		inflater.inflate(R.menu.menu, menu);
		// できたらtrueを返す
		return ret;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return super.onPrepareOptionsMenu(menu);
	}

	// Option Menu のアイテムが選択された時の動作
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		boolean ret = super.onOptionsItemSelected(item);
		// 選ばれたアイテムの ID を取得
		switch (item.getItemId()) {
		case R.id.myKaguList:
			Intent intent = new Intent(getApplicationContext(),
					MyKaguListActivity.class);
			intent.putExtra(MadoriActivity.this.getString(R.string.heyaKey),
					heya);
			startActivity(intent);
			return true;
		case R.id.madoriSave:
			return true;
		default:
		}
		return ret;
	}

	// Option Menu が開かれた時の動作
	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		return super.onMenuOpened(featureId, menu);
	}
}
